package com.okmin.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * https://github.com/hss01248/DialogUtil
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Context mContext;
    private Unbinder mBind;
    protected String TAG=getClass().getSimpleName();
    private View mToolbar;

    /**
     *
     * @return 布局中替换Fragment的ID
     */
    protected  int getReplaceFragmentContentId(){
        return R.id.fragment_container;
    }

    //添加fragment
    protected void addFragment(BaseFragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.push_left_in,
                    R.anim.push_left_out,R.anim.push_right_in,R.anim.push_right_out)
            .replace(getReplaceFragmentContentId(), fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    //移除fragment
    protected void removeFragment() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    //返回键返回事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
                finish();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        View contentView = getLayoutInflater().inflate(getLayoutId(), null);
        ((LinearLayout)findViewById(R.id.fragment_container)).addView(contentView);
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setVisibility(getToolbarVisbility()?View.VISIBLE:View.GONE);
        mContext = this;
        mBind = ButterKnife.bind(this);
        initsystembar();
        initView();
        initData();
    }

    protected boolean getToolbarVisbility() {
        return true;
    }

    protected abstract int getLayoutId();

    protected abstract void initData();

    protected abstract void loadData();

    protected  void initView(){}

    private void initsystembar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            SystemBarTintManager tintManager = new SystemBarTintManager(this);

            tintManager.setStatusBarTintEnabled(true);

            tintManager.setStatusBarTintResource(getSystemBarcolor());
        }
    }

    /**
     * 透明色,会使得颜色和布局中的一样
     * @return
     */
    protected int getSystemBarcolor() {
        return android.R.color.transparent;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBind.unbind();
    }

    /**
     * 打开动画
     */
    public void rightToLeft(){
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    /**
     * 关闭动画
     */
    public void leftToRight(){
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        rightToLeft();
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
        rightToLeft();
    }

    @Override
    public void finish() {
        super.finish();
        leftToRight();
    }
}
